package com.moli.iov.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.model.dto.req.PageConvert;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.sys.mapper.UserMapper;
import com.moli.iov.sys.model.dto.req.UserDto;
import com.moli.iov.sys.model.po.User;
import com.moli.iov.sys.service.IUserService;
import com.moli.iov.util.StringUtil;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  用户业务
 * </p>
 *
 * @author zql
 * @since 2019/9/9 11:22
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

  @Autowired
  private UserMapper userMapper;

  /**
   * 分页查询用户列表
   * @param userDto
   * @return
   */
  @Override
  public PageExtend<User> listUserOfPage(UserDto userDto) {
    IPage<User> page =
        userMapper.listUserOfPage(new Page<User>(userDto.getPageNum(), userDto.getPageSize()), userDto);
    return PageConvert.convert(page);


  }

  /**
   * 用户列表
   * @param userDto
   * @return
   */
  @Override
  public List<User> listUsers(UserDto userDto) {
    return super.list(assemblyConditions(userDto));
  }

  /**
   * 根据用户Id查询用户信息
   * @param userId
   * @return
   */
  @Override
  public User getUserByUserId(Long userId) {
    return super.getById(userId);
  }

  /**
   * 根据账号查询用户信息
   * @param account
   * @return
   */
  @Override
  public User getUserByAccount(String account) {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("account", account);
    return super.getOne(queryWrapper);
  }

  /**
   * 添加用户
   * @param user
   * @return
   */
  @Override
  public User saveUser(User user) {
    super.save(user);
    return user;
  }

  /**
   * 修改用户/重置密码
   * @param user
   * @return
   */
  @Override
  public boolean updateUser(User user) {
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("user_id", user.getUserId());
    return super.update(user, queryWrapper);
  }

  /**
   * 组装查询条件
   * @param userDto
   * @return
   */
  private QueryWrapper<User> assemblyConditions(UserDto userDto){
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    if(userDto == null){
      return queryWrapper;
    }

    if(userDto.getNeUserId() != null){
      queryWrapper.ne("user_id", userDto.getNeUserId());
    }

    if(userDto.getUserIds() != null && !userDto.getUserIds().isEmpty()){
      queryWrapper.in("user_id", userDto.getUserIds());
    }

    if(StringUtil.isNotBlank(userDto.getAccount())){
      queryWrapper.eq("account", userDto.getAccount());
    }

    if(userDto.getNeCompanyId() != null){
      queryWrapper.ne("company_id", userDto.getNeCompanyId());
    }

    if(StringUtil.isNotBlank(userDto.getEmail())){
      queryWrapper.eq("email", userDto.getEmail());
    }

    if(StringUtil.isNotBlank(userDto.getRealName())){
      queryWrapper.eq("real_name", userDto.getRealName());
    }

    if(userDto.getStatus() != null){
      queryWrapper.eq("status", userDto.getStatus());
    }

    if(userDto.getCompanyId() != null){
      queryWrapper.eq("company_id", userDto.getCompanyId());
    }

    if(StringUtil.isNotBlank(userDto.getUserPhone())){
      queryWrapper.eq("user_phone", userDto.getUserPhone());
    }

    queryWrapper.orderByDesc("create_time");
    return queryWrapper;
  }
}
